using System;
using System.IO;
using System.Web;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.Policy;

namespace PHSRAG.Insight21.Humans
{
    /// <summary>
    /// Summary description for FileUploadPage.
    /// </summary>
    public partial class FileUploadPage : NoSessionHeartbeatPage
    {
        private ICachePolicy _cachePolicy;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                //The frame inside which this page is loaded is submitted only if the electronic mode is chosen.
                if (Request.Files.Count > 0)
                {
                    HttpPostedFile fileUploaded = Request.Files[0];
                    if (fileUploaded.ContentLength > 0)
                    {
                        string newFileName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());

                        fileUploaded.SaveAs(newFileName);
                        _cachePolicy.Cache(attachmentTypeNTitle.Value, string.Format("{0}|{1}", fileUploaded.FileName, newFileName));
                        Page.ClientScript.RegisterStartupScript(GetType(), "UploadedFile", "<script>window.parent.FileUploadComplete('" + attachmentTypeNTitle.Value.Replace("'", "\\'") + "');</script>");
                    }
                }
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
            _cachePolicy = (Policy.ICachePolicy) Session[Policy.Names.CachePolicyKey];
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }

        #endregion
    }
}